from fastapi import APIRouter

from db import query_result
from schema import SuccessResponse

xcx = APIRouter(tags=["uniapp小程序学习"])
domain = 'http://demo.dtcms.net'


@xcx.get("/getlunbo")
async def getlunbo() -> dict:
    """
    获取轮播图列表
    :return: 轮播图列表
    """
    data = SuccessResponse(message=[
        {
            "id": 1,
            "url": "http://www.itcast.cn/subject/phoneweb/index.html",
            "img": "http://m.itheima.com/images/slidead/mobile/20191213180241750x410.jpg"
        },
        {
            "id": 2,
            "url": "http://www.itcast.cn/subject/phoneweb/index.html",
            "img": "http://m.itheima.com/images/slidead/mobile/20191210154717750-410.jpg"
        },
        {
            "id": 3,
            "url": "http://www.itcast.cn/subject/phoneweb/index.html",
            "img": "http://m.itheima.com/images/slidead/mobile/20190327135101750x410-%E4%BC%A0%E6%99%BA%E9%BB%91%E9%A9%AC%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%B9%BB%E7%81%AF.jpg"
        }
    ])
    return data


@xcx.get("/getgoods")
async def getgoods(pageindex: int = 1) -> dict:
    """
    获取推荐商品列表
    :param pageindex: 页码
    :return:
    """
    pagesize = 10
    if pageindex != 0:
        skipcount = (pageindex - 1) * pagesize
        sql = f"SELECT a.id,a.title,a.add_time,left(a.zhaiyao,25) as zhaiyao,a.click,concat('{domain}',a.img_url) as img_url,b.sell_price,b.market_price,b.stock_quantity FROM dt_article as a,dt_article_attribute_value b where a.id = b.article_id and a.channel_id = 7 limit {skipcount},{pagesize}"
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/getimgcategory")
async def getimgcategory():
    """
    获取图片分类列表
    :return:
    """
    sql = ' select title,id from dt_article_category where channel_id = 9 '
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/getimages/{cateid}")
async def getimages(cateid: int) -> dict:
    """
    二级图片列表
    :param cateid: 图片类别id
    :return:
    """
    sql = ' select id,title,img_url,zhaiyao from dt_article where channel_id = 9 and category_id=' + str(cateid)
    if cateid <= 0:
        sql = ' select * from dt_article where channel_id = 9 '
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/getnewslist")
async def getnewslist() -> dict:
    """
    获取资讯列表
    :return: []
    """
    sql = " SELECT id,title,add_time,left(zhaiyao,25) as zhaiyao,click,concat('" + domain + "',img_url) as img_url FROM dt_article where img_url > '' and channel_id = 6 limit 0,10 "
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/getnew/{newid}")
async def getnew(newid: int) -> dict:
    """
    根据资讯id获取资讯详细内容
    :param newid:
    :return:
    """
    sql = 'select id,title,click,add_time,content from dt_article  where id=' + str(newid)
    print(sql)
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/getnews")
async def getnew(newid) -> dict:
    """
    该接口是为了用来学习sql注入使用
    根据资讯id获取资讯详细内容
    :param newid:
    :return:
    """
    sql = 'select id,title,click,add_time,content from dt_article  where id=' + newid
    print(sql)
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/getthumimages/{imgid}")
async def getthumimages(imgid: int):
    """
    商品详情轮播图
    :param imgid:
    :return:
    """
    sql = f"select thumb_path as src  from dt_article_albums where article_id ={str(imgid)}"
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/goods/getinfo/{id}")
async def getgoodsinfo(id: int):
    """
    商品详情中的价格、库存等信息
    :param id:
    :return:
    """
    sql = f"SELECT da.id,da.title,da.add_time,daa.goods_no,daa.stock_quantity,daa.market_price,daa.sell_price FROM dt_article da , dt_article_attribute_value daa WHERE  da.id = daa.article_id and da.id = {id}"
    data = query_result(sql)
    return SuccessResponse(message=data)


@xcx.get("/goods/getdesc/{id}")
async def getgooddesc(id: int):
    """
    商品详情的图文描述内容
    :param id: 商品id
    :return:
    """
    sql = f" SELECT title,content FROM dt_article da WHERE da.id = {id}"
    data = query_result(sql)
    return SuccessResponse(message=data)